﻿using CC.Models;
using Dapper;
using System;
using System.Collections.Generic;
using System.Text;

namespace CC.BLLs
{
    public class CommentStatisticsBLL
    {
        public static void PlusComment(long commentId)
        {
            using (var conn = DatabaseHelper.Get())
            {
                var rootComment = conn.QueryFirstOrDefault<Comment>($"select * from Comment where CommentId={commentId}");
                var calcCommentId = rootComment.ParentId;
                while (true)
                {
                    if (calcCommentId > 0)
                    {
                        var comment = conn.QueryFirstOrDefault<Comment>($"select * from Comment where CommentId={calcCommentId}");
                        WritePlusComment(comment.OutId, false);
                        calcCommentId = comment.ParentId;
                    }
                    else
                    {
                        break;
                    }
                }
                WritePlusComment(rootComment.OutId, true);

            }
        }

        private static void WritePlusComment(string outId, bool hasDirect)
        {
            using (var conn = DatabaseHelper.Get())
            {
                using (var tran = conn.BeginTransaction())
                {
                    var statistics = conn.QueryFirstOrDefault<CommentStatistics>("select * from comment_statistics where OutId=@OutId for update", new { OutId = outId }, transaction: tran);
                    if (statistics != null)
                    {
                        conn.Execute($"update comment_statistics set  CommentCount=CommentCount+1{(hasDirect ? ",DirectCommentCount=DirectCommentCount+1" : "")} where OutId=@OutId", new { OutId = outId }, transaction: tran);
                    }
                    else
                    {
                        conn.Execute(@"INSERT INTO  `comment_statistics` (
                                              `OutId`,
                                              `CommentCount`,
                                              `LikeCount`,
                                              `DirectCommentCount` 
                                            ) 
                                            VALUES
                                              (
                                                @OutId,
                                                @CommentCount,
                                                @LikeCount,
                                                @DirectCommentCount 
                                              )",
                                              new CommentStatistics()
                                              {
                                                  OutId = outId,
                                                  CommentCount = 1,
                                                  DirectCommentCount = hasDirect ? 1 : 0
                                              }, transaction: tran);
                    }
                    tran.Commit();
                }
            }
        }

        public static void PlusLike(string outId)
        {
            using (var conn = DatabaseHelper.Get())
            {
                using (var tran = conn.BeginTransaction())
                {
                    var statistics = conn.QueryFirstOrDefault<CommentStatistics>("select * from comment_statistics where OutId=@OutId for update", new { OutId = outId }, transaction: tran);
                    if (statistics != null)
                    {
                        conn.Execute($"update comment_statistics set  LikeCount=LikeCount+1  where OutId=@OutId", new { OutId = outId }, transaction: tran);
                    }
                    else
                    {
                        conn.Execute(@"INSERT INTO  `comment_statistics` (
                                              `OutId`,
                                              `CommentCount`,
                                              `LikeCount`,
                                              `DirectCommentCount` 
                                            ) 
                                            VALUES
                                              (
                                                @OutId,
                                                @CommentCount,
                                                @LikeCount,
                                                @DirectCommentCount 
                                              )",
                                              new CommentStatistics()
                                              {
                                                  OutId = outId,
                                                  LikeCount = 1,
                                              }, transaction: tran);
                    }
                    tran.Commit();
                }
            }
        }

        public static List<CommentStatistics> List(List<string> outIds)
        { 
            using (var conn = DatabaseHelper.Get())
            {
                return conn.Query<CommentStatistics>("select * from comment_statistics where OutId  in @OutIds", new { OutIds = outIds }).AsList();
            }
        }

        public static void SubLike(string outId)
        {
            using (var conn = DatabaseHelper.Get())
            {
                conn.Execute($"update comment_statistics set  LikeCount=LikeCount-1  where OutId=@OutId  and LikeCount>0", new { OutId = outId });
            }
        }
    }
}
